﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class chitiettintuc : System.Web.UI.Page
{
    ConnectionData sqlconn = new ConnectionData();
    protected void Page_Load(object sender, EventArgs e)
    {
        TinChitietDiaOc();
        if (!IsPostBack)
        {
            CapNhatLuotXem();
        }
    }
    protected void dtlchitietdiaoc_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        dtlChitietDiaoc(sender, e);
    }
    public void CapNhatLuotXem()
    {

        int iddt = Convert.ToInt32(this.Request["id"]);

        String sqliddo = "select dia_oc_id from Dang_Tin where id = " + iddt;
        int iddo = sqlconn.LayId(sqliddo);

        String sqlluotxem = "select So_Lan_Xem from Dia_Oc where id =" + iddo;
        int LuotXem = sqlconn.LayLuotXem(sqlluotxem);
        LuotXem++;

        
        String sql = "update Dia_Oc set So_Lan_Xem=" + LuotXem + " where id ="+iddo;
        sqlconn.ExecuteSQL(sql);
        
    }
    public void TinChitietDiaOc()
    {
        int id = Convert.ToInt32(this.Request["id"]);
        String sql;
        
        sql = "select top 1 dt.Mo_ta, dt.tieu_de, dt.Ngay_Bat_Dau, dc.So_Nha, dc.Duong_Pho, p.Phuong_Xa, q.Quan_Huyen, tp.Ten_TP,dt.Thuong_Luong, do.ID, do.Hinh_Anh, do.So_Lau, do.So_Phong_ngu, do.so_phong_khach, do.so_phong_tam, do.so_phong_tam, do.so_phong_khac, do.DTKV, do.DTSD, do.So_Lan_Xem, do.Gia, do.Lien_He, do.Phap_Ly, do.Tong_Dien_Tich, do.Huong, do.Dac_Diem from DANG_TIN as dt, DIA_OC as do, DIA_CHI as dc, QUAN_HUYEN as q, PHUONG_XA as p, THANH_PHO as tp where dt.Dia_Oc_ID = do.Id and dc.TP_Id = tp.Id and q.TP_Id = tp.Id and p.Quan_id = q.Id and dc.Quan_Id = q.Id and dc.Phuong_id = p.Id and do.Dia_Chi_ID = dc.Id and dt.id = " + id + " order by dt.id desc";

        DataSet ds = new DataSet();
        ds = sqlconn.ExecuteQuery(sql);
        dtlchitietdiaoc.DataSource = ds;
        dtlchitietdiaoc.DataBind();

    }

    protected void dtlChitietDiaoc(object sender, DataListItemEventArgs e)
    {
        //Lay id diaoc
        
        //tin moi nhat
        Image img = default(Image);
        img = (Image)e.Item.FindControl("imgHinh");
        img.ImageUrl = "~/HinhDiaOc/" + Convert.ToString(((DataRowView)e.Item.DataItem)["Hinh_Anh"]);

        //vi tri

        Label vitri = default(Label);
        vitri = (Label)e.Item.FindControl("lblVitri");
        String SoNha = Convert.ToString(((DataRowView)e.Item.DataItem)["so_nha"]);
        String Duong = Convert.ToString(((DataRowView)e.Item.DataItem)["duong_pho"]);
        String Phuong = Convert.ToString(((DataRowView)e.Item.DataItem)["phuong_xa"]);
        String Quan = Convert.ToString(((DataRowView)e.Item.DataItem)["quan_huyen"]);
        String TP = Convert.ToString(((DataRowView)e.Item.DataItem)["ten_tp"]);
        vitri.Text = SoNha + " - " + Duong + " - " + Phuong + " - " + Quan + " - " + TP;

        //tieu de tin dang
        Label tieude = default(Label);
        tieude = (Label)e.Item.FindControl("lblTieude");
        tieude.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["tieu_de"]);
        

        //ngay dang
        Label ngaydang = default(Label);
        ngaydang = (Label)e.Item.FindControl("lblNgayDang");
        ngaydang.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["ngay_bat_dau"]);

        //DTKV
        Label DTKV = default(Label);
        DTKV = (Label)e.Item.FindControl("lblDTKV");
        DTKV.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["DTKV"]);

        //DTSD
        Label DTSD = default(Label);
        DTSD = (Label)e.Item.FindControl("lblDTSD");
        DTSD.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["DTSD"]);

        //Cau Truc
        Label Lau = default(Label);
        Lau = (Label)e.Item.FindControl("lblSoLau");
        Lau.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["so_lau"]);

        //phong khach
        Label PhongKhach = default(Label);
        PhongKhach = (Label)e.Item.FindControl("lblPhongKhach");
        PhongKhach.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["so_phong_khach"]);
        //Phong ngu
        Label PhongNgu = default(Label);
        PhongNgu = (Label)e.Item.FindControl("lblPhongNgu");
        PhongNgu.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["so_phong_ngu"]);
        //Phong tam
        Label PhongTam = default(Label);
        PhongTam = (Label)e.Item.FindControl("lblPhongTam");
        PhongTam.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["so_phong_tam"]);
        //phong khac
        Label PhongKhac= default(Label);
        PhongKhac = (Label)e.Item.FindControl("lblPhongKhac");
        PhongKhac.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["so_phong_khac"]);
        

        //Gia
        Label Gia = default(Label);
        Gia = (Label)e.Item.FindControl("lblGia");
        Gia.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Gia"]);

        //Thuong luong
        Label ThuongLuong = default(Label);
        ThuongLuong = (Label)e.Item.FindControl("lblThuongLuong");
        ThuongLuong.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Thuong_Luong"]);

        //So lan xem
        Label LuotXem = default(Label);
        LuotXem = (Label)e.Item.FindControl("lblLuotXem");
        LuotXem.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["So_Lan_Xem"]);
        //Mô tả
        Label MoTa = default(Label);
        MoTa = (Label)e.Item.FindControl("lblMoTa");
        MoTa.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Mo_Ta"]);

        //Liên hệ
        Label LienHe = default(Label);
        LienHe = (Label)e.Item.FindControl("lblLienHe");
        LienHe.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Lien_He"]);

        //Tổng diện tích
        Label TongS = default(Label);
        TongS = (Label)e.Item.FindControl("lblTongDT");
        TongS.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Tong_Dien_Tich"]);

        //Huong
        Label Huong= default(Label);
        Huong = (Label)e.Item.FindControl("lblHuong");
        Huong.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Huong"]);

        //Tình trạng pháp lý
        Label PhapLy = default(Label);
        PhapLy = (Label)e.Item.FindControl("lblPhapLy");
        PhapLy.Text = Convert.ToString(((DataRowView)e.Item.DataItem)["Phap_Ly"]);

        
        int iddo = Convert.ToInt32(((DataRowView)e.Item.DataItem)["ID"]);
        //dac diem
        String sql;
        sql = "select count(*) from Dia_Oc where Dac_Diem like '%Internet%' and id="+iddo;
        Label Internet = default(Label);
        Internet = (Label)e.Item.FindControl("lblInternet");
        if (sqlconn.ktTonTai(sql) != 0)
            Internet.Text = "Có";
        sql = "select Count(*) from Dia_Oc where Dac_Diem like '%Gần trường%' and id="+iddo;
        Label GanTruong = default(Label);
        GanTruong = (Label)e.Item.FindControl("lblGanTruong");
        if (sqlconn.ktTonTai(sql) != 0)
            GanTruong.Text = "Có";

        sql = "select Count(*) from Dia_Oc where Dac_Diem like '%Gần chợ%' and="+iddo;
        Label GanCho = default(Label);
        GanCho = (Label)e.Item.FindControl("lblGanCho");
        if (sqlconn.ktTonTai(sql) != 0)
            GanCho.Text = "Có";

        sql = "select Count(*) from Dia_Oc where Dac_Diem like '%Sân vườn%' and id="+iddo;
        Label SanVuon = default(Label);
        SanVuon = (Label)e.Item.FindControl("lblSanVuon");
        if (sqlconn.ktTonTai(sql) != 0)
            SanVuon.Text = "Có";

        sql = "select Count(*) from Dia_Oc where Dac_Diem like '%Bãi giữ xe%' and id="+iddo;
        Label ChoDauXeHoi = default(Label);
        ChoDauXeHoi = (Label)e.Item.FindControl("lblChoDauXeHoi");
        if (sqlconn.ktTonTai(sql) != 0)
            GanCho.Text = "Có";
        
        //ket thuc tin moi nhat
    }
}